import {getTeacher,createTeacher,deleteTeacher,updateTeacher} from '../services/teachers'

function combine(state,newS){
    let newState = {...state};
    Object.keys(newS).forEach((k)=>{
        let v = newS[k];
        if(v instanceof Object){
            newState[k]={...newState[k],...v}
        }else{
            newState[k]=v;
        }
    })
    return newState;
}
function delay(timeout){
  return new Promise(resolve => {
    setTimeout(resolve, timeout);
  });
}
export default {
  namespace: 'teacher',
  state: {
      teachers:[],
      /***
        name 姓名
        workId 考勤工号
        phoneNumber 手机号
      ***/
      loading:false,
      pagination: {
	        showSizeChanger: true,
	        showQuickJumper: true,
	        showTotal: total => `共 ${total} 条`,
	        current: 1,
	        total: null
      },
      modal:{
          show:false,
          loading:false,
          modify:null
      }
  },
  reducers: {
      updateTeachers(state,{payload:{teachers}}){
          return {...state,teachers:[...state.teachers,...teachers]}
      },
      removeTeacher(state,{payload}){
          return {...state,teachers:state.teachers.filter(r=>r.id!=payload)}
      },
      showLoading(state){
        return { ...state, loading: true };
      },
      hideLoading(state){
        return { ...state, loading: false };
      },
      showModal(state){
          let r = {...state};
          r.modal.show=true;
          return r;
      },
      startModify(state,{payload}){
          return {...state,modal:{loading:false,show:true,modify:{...payload}}}
      },
      endMofidy(state,{payload}){
          return  {...state,
              teachers:[...state.teachers.filter(v=>v.id!=payload.id),payload],
              modal:{show:false,loading:false,modify:null}};
      },
      hideModal(state){
          let r = {...state};
          r.modal.show=false;
          return r;
      },
    //   updateTeacher(state,{payload}){
    //       let r = {...state};
    //       r.teachers=payload;
    //       return r;
    //   },
      showModalLoading(state){
          return combine(state,{modal:{loading:true}});
      },
      hideModalLoading(state){
          return combine(state,{modal:{loading:false}});
      }
  },
  effects: {
      *query({},{call,put}){
          yield put({type:"showLoading"});
          let teachers = yield call(getTeacher);
          yield put({type:"hideLoading"});
          yield put({type:"updateTeachers",payload:{teachers}});
      },
      *create({payload},{call,put}){
        //   yield put({type:"showLoading"});
          yield put({type:"showModalLoading"});
          let result = yield call(createTeacher,payload);
        //   yield put({type:"hideLoading"});
        //   console.log(result);
          if(result.success){
              yield put({type:'updateTeachers',payload:{teachers:[{
                  ...payload,
                  id:result.data.id,
              }]}})
          }
          yield put({type:"hideModalLoading"});
          yield put({type:"hideModal"});

      },
    *delete({payload},{call,put}){
        yield put({type:"showLoading"});
        let result = yield call(deleteTeacher,{id:payload});
        if(result.success){
            yield put({type:'removeTeacher',payload})
        }
        yield put({type:"hideLoading"});
    },
    *modify({payload},{call,put}){
        // yield put({type:"startModify",payload});
        yield put({type:'showModalLoading'});
        let re = yield call(updateTeacher,payload);
        // console.log(re);
        if(re.success){

            yield put({type:'endMofidy',payload});
        }else{
            yield put({type:'hideModalLoading'});
        }

    }
  },
  subscriptions: {
      setup ({ dispatch, history }){
          dispatch({
            type: 'query',
          });
        //   dispatch({
        //       type:'create'
        //   })
        //   console.log('helo');
      }
  },
};
